#include "GfxD3D9.h"

using imGraphics::D3D9Texture;
using std::string;

LPDIRECT3DTEXTURE9 imGraphics::g_Tex[NUMTEXTURES];

D3D9Texture::D3D9Texture() {
	m_Tex = NULL;
}

D3D9Texture::~D3D9Texture() {
	Release();
}

bool D3D9Texture::ReopenFile() {
	Release();
	LoadFromFile(m_FilePath);
	return true;
}

void D3D9Texture::LoadFromFile(std::string &FilePath) {
	HRESULT hr = D3DXCreateTextureFromFile(g_DeviceD3D9, FilePath.c_str(), &m_Tex);
	if(SUCCEEDED(hr))
		m_FilePath = FilePath;
}

void D3D9Texture::FillSolid(Vector4 &Col) {
	int s = 8; // The width and height of this solid texture

	Release(); // Clear the texture if we already have one
	g_DeviceD3D9->CreateTexture(s, s, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &m_Tex, NULL);

	DWORD Colour = D3DXCOLOR(Col.x, Col.y, Col.z, Col.w);
	
	D3DLOCKED_RECT MyRect;
	HRESULT hr = m_Tex->LockRect(0, &MyRect, NULL, 0);
	
	// Filling in the texture
	DWORD* pBits = (DWORD*)MyRect.pBits;
	for(int i = 0; i < s*s; i++)
		pBits[i] = Colour;

	m_Tex->UnlockRect(0);
}

void D3D9Texture::Release() {
	if(m_Tex == NULL) return;
	m_Tex->Release();
	m_Tex = NULL;
}

void D3D9Texture::UseTexture(int Channel) {
	g_Tex[Channel] = m_Tex;
}

/*
class DECLDIR D3D9Texture : public iTexture {
private:
	LPDIRECT3DTEXTURE9 m_Tex;
public:
	bool ReopenFile();

	void LoadFromFile(char* Path);
	void Release();
	
	void UseTexture(int Channel);
};
*/